package com.rose.datax.dataxadmin.utils;

import java.util.ArrayList;
import java.util.List;

public class ArrayUtils {

    /**
     * 把一个list分成几个数组
     * @param tables
     * @param <T>
     * @return
     */
    public static <T> List<List<T>> splitArray(List<T> tables){
        int split = 4;
        int size = tables.size();
        if(size > 10 && size <= 20){
            split = 6;
        }else if(tables.size() > 20){
            split = 8;
        }
        List<List<T>> result = new ArrayList<List<T>>();
        int remainder = tables.size() % split;
        int number = tables.size() / split;
        int offset = 0;
        for (int i = 0; i < split; i++) {
            List<T> value = null;
            if (remainder > 0) {
                value = tables.subList(i * number + offset, (i + 1) * number + offset + 1);
                remainder--;
                offset++;
            }else{
                value = tables.subList(i * number + offset, (i + 1) * number + offset);
            }
            result.add(value);
        }
        return result;
    }

}
